$(function(){
    $("#form-member-add").validate({
        rules:{
            topic:{
                required:true
            },
            /* activityUrl:{
                 required:true
             },*/
            name:{
                required:true
            },
            start:{
                required:true
            },
            end:{
                required:true
            },
        },
        onkeyup:false,
        focusCleanup:true,
        success:"valid",
        submitHandler:function(form){
            var discountsPrice=$("#discountsPrice").val();
            $("#discountsPrice").val(discountsPrice*100);
            $("#form-member-add").ajaxSubmit({
                cache:true,
                type:'POST',
                dataType: 'json',
                data:$('#form-member-add').serialize(),
                url:'/admin/distributionConfig/modifiyDistributionConfig.html',
                async: false,
                success:function(data){
                    console.log(data);
                    if (data.code != 1){
                        layer.msg(data.msg,{icon : 5,time : 1000});
                        console.log(data.msg);
                        return;
                    }
                    layer.msg('操作成功!',{icon : 6,time : 1000},function(){
                        window.parent.location.reload();
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    })
                }
            })
        }
    });
});

function delDistributionConfigById(tid){
    layer.confirm('确认删除吗？',function(index){
        // var postData = {"activityId": activityId,"activityName": activityName,"activityUrl": activityUrl};
        $.ajax({
            cache:true,
            type:'POST',
            url:'/admin/distributionConfig/delDistributionConfigById.html?tid='+tid,
            dataType: 'json',
            async: false,
            success:function(data){
                console.log(data);
                // alert(data.code);
                if (data.code != 1){
                    layer.msg('操作失败!',{icon : 5,time : 1000});
                    console.log(data.msg);
                    return;
                }
                layer.msg('操作成功!',{icon : 6,time : 1000},function(){
                    location.reload();
                    // var index = parent.layer.getFrameIndex(window.name);
                    // parent.layer.close(index);
                })
            }
        })
    });
}

//根据产品名称查询产品
/*function setActivityProduct(activityId) {
    var divhtml = '<div id="sellerList">';
    divhtml += '<div class="row cl">';
    divhtml += ' <div class="formControls col-xs-8 col-sm-9">';
    divhtml += '<span class="select-box">';
    divhtml += '  <input type="hidden"  id="activityId">';
    divhtml += '  <input type="hidden"  id="productEntityId">';
    divhtml += '  <input type="text" class="input-text"  placeholder="输入产品类型名称" id="productEntityName1">';
    divhtml += '</span>';
    divhtml += '</div>';
    divhtml += '</div> <div class="row cl">';
    divhtml += '<div class="formControls col-xs-8 col-sm-9">';
    divhtml += '<table id="tab" class="table table-border table-bordered table-bg table-hover table-sort">';
    divhtml += '<thead>';
    divhtml += '<tr class="text-c">';
    divhtml += ' <th width="12%">产品类型编号</th>';
    divhtml += ' <th width="12%">产品类型名称</th>';
    divhtml += '<th width="12%">产品类型介绍</th>';
    divhtml += '        </tr>';
    divhtml += '</thead>';
    divhtml += '<tbody id="orgList">';
    divhtml += '</tbody>';
    divhtml += ' </table>';
    divhtml += '</div>';
    divhtml += '</div>';
    divhtml += '  <div class="row cl">';
    divhtml += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">';
    divhtml += '<input class="btn btn-primary radius" type="submit" id="sellerNameSearch" value="&nbsp;&nbsp;搜索&nbsp;&nbsp;">';
    divhtml += '&nbsp;&nbsp;&nbsp;&nbsp;';
    divhtml += '<input class="btn btn-primary radius" type="submit" id="sellerNameSubmit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">';
    divhtml += '</div>';
    divhtml += '</div>';
    divhtml += '</div>';

    layerTools.layerShowDIv(divhtml, '');
    $("#activityId").val(activityId);
    var postUrl = ctx + '/admin/product/getProductEntityByName.html';
    //var postData = {"productEntityName": $("#productEntityName1").val()};
    ajaxRequestMethod("sellerNameSearch",postUrl,1);
}

function getProductData(productEntityId, productName) {
    //选中颜色变化
    var trs = $("#orgList").children("tr")
    for( var i=0; i<trs.length; i++ ){
        trs[i].onmousedown = function(){
            tronmousedown(this);
        }
    }
    //参数设置
    $("#productEntityId").val(productEntityId);
    var postUrl = ctx + '/admin/distributionConfig/modifiyDistributionConfig.html';
    ajaxRequestMethod("sellerNameSubmit",postUrl,2);

}

function ajaxRequestMethod(fieldName,postUrl,status){
    $("#"+fieldName).unbind();
    $("#"+fieldName).bind("click", function () {
        if(status==1){
            var postData = {"productEntityName": $("#productEntityName1").val()};
        }
        if(status==2){
            var postData = {"tid": $("#activityId").val(),"productEntityId": $("#productEntityId").val(),"flag": "flag"};
        }
        $.ajax({
            type: 'POST',
            url: postUrl,
            dataType: 'json',
            data: postData,
            success: function (data) {
                console.log(data);
                if (data.code != 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000});
                    return;
                }
                if(status==1){
                    var sellerList = data.data;
                    var html = "";
                    if(sellerList==0){
                        html +="<tr><td colspan ='2'><center>没有匹配数据！</center></td></tr>";
                    }
                    for (i = 0; i < sellerList.length; i++) {
                        html += "  <tr onclick='getProductData(\"" + sellerList[i].productEntityId + "\",\"" + sellerList[i].productEntityName + "\")'><td >" + sellerList[i].productEntityId + "</td><td >" + sellerList[i].productEntityName + "</td><td>" + isemptyStr(sellerList[i].productEntityIntroduction) + "</td></tr>";
                    }
                    $("#orgList").html(html);
                }
                if(status==2){
                    if (data.code != 1){
                        layer.msg(data.msg,{icon : 5,time : 2000});
                        console.log(data.msg);
                        return;
                    }
                    layer.msg('操作成功!',{icon : 6,time : 2000});
                    setTimeout(function(){layer.closeAll()}, 3000);
                    window.location.reload();
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        })
    });
}*/

function tronmousedown(obj){
    var trs = $("#orgList").children("tr")
    for( var o=0; o<trs.length; o++ ){
        if( trs[o] == obj ){
            trs[o].style.backgroundColor = '#DFEBF2';
        }
        else{
            trs[o].style.backgroundColor = '';
        }
    }
}